package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbdgj.R;
import com.wbdgj.adapter.LGRecycleViewAdapter;
import com.wbdgj.adapter.LGViewHolder;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.SpacesItemDecoration;
import com.wbdgj.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity{

    @BindView(R.id.search_edit)
    EditText search_edit;
    @BindView(R.id.qingkong)
    ImageView qingkong;

    @BindView(R.id.search_list)
    RecyclerView search_list;

    private List<String> stringList;
    private String search_hostory;
    private MyAdapter myAdapter;
    private String searchText, COORDSX, COORDSY;
    private Context context = this;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_search;
    }

    @Override
    protected void initView() {
        stringList = new ArrayList<>();
        search_hostory = BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.SEARCH_HISTORY);
        if (search_hostory != null && !search_hostory.isEmpty()) {
            String[] arr = search_hostory.split(",");
            for (int i = 0; i < (arr.length > 10 ? 10 : arr.length); i++) {
                stringList.add(arr[i]);
            }
        }
        setGridLayout(search_list, this, 4);
        myAdapter = new MyAdapter(stringList);
        search_list.setAdapter(myAdapter);
        search_list.addItemDecoration(new SpacesItemDecoration(15));
        search_edit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (KeyEvent.KEYCODE_ENTER == keyCode && event.getAction() == KeyEvent.ACTION_UP) {
                    // do some your things
                    if(!TextUtils.isEmpty(search_edit.getText().toString())){
                        searchText = search_edit.getText() + "";
                        if (!stringList.contains(searchText)) {
                            if (!search_hostory.isEmpty()) {
                                search_hostory = "," + search_hostory;
                            }
                            search_hostory = searchText + search_hostory;
                            BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.SEARCH_HISTORY, search_hostory);
                            stringList.add(0, searchText);
                            myAdapter.refreshDate(stringList);
                            myAdapter.notifyDataSetChanged();
                        }
                        startActivity(new Intent(context, SearchResultActivity.class).putExtra("key", searchText));
                    }else{
                        ToastUtils.toastShort("请输入关键字！");
                    }

                }
                return false;
            }
        });

        qingkong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.SEARCH_HISTORY, "");
                stringList.clear();
                myAdapter.refreshDate(stringList);
                myAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick({R.id.gbLay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.gbLay:
                finish();
                break;
        }
    }

    public class MyAdapter extends LGRecycleViewAdapter<String> {

        public MyAdapter(List<String> dataList) {
            super(dataList);
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_search_list;
        }

        @Override
        public void convert(LGViewHolder holder, final String s, int position) {
            TextView textView = (TextView) holder.getView(R.id.text_view);
            textView.setText(s);
            holder.getView(R.id.type_linear).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (s.equals("清空记录")) {
                        BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.SEARCH_HISTORY, "");
                        stringList.clear();
                        myAdapter.refreshDate(stringList);
                        myAdapter.notifyDataSetChanged();
                    } else {
                        startActivity(new Intent(context, SearchResultActivity.class).putExtra("key", s));
                    }
                }
            });
        }
    }

    public void setGridLayout(RecyclerView pullRecycleView, Context mContext, int spanCount) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }
}
